package de.zainodis.triplog.data.persistence;

import android.content.Context;

import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.DatabaseTableConfig;

import java.sql.SQLException;

import de.zainodis.triplog.data.TripEntry;

/**
 * Created by fzarrai on 9/9/2014.
 */
public interface ITripDao extends Dao<TripEntry, Long> {

    //Table name
    public static final String TABLE_NAME = "trip";
    //ID for Trip
    public static final String COLUMN_ID = "_id";
    //Start date of trip
    public static final String COLUMN_DATE_END = "end";
    //End date of trip
    public static final String COLUMN_DATE_START = "start";
    /**
     * This method is used by the content provider to insert a new trip if it does not already exist.
     *
     * @param context Activity context required to obtain other relevant dao objects.
     * @param trip    the trip to create if it does not already exist.
     * @return the database id.
     */
    public long createIfNotExists(Context context, TripEntry trip) throws SQLException;

    public class TripDao extends BaseDaoImpl<TripEntry, Long> implements ITripDao {
        private static final String TAG = "TripDao";


        public TripDao(ConnectionSource connectionSource) throws SQLException {
            super(connectionSource, TripEntry.class);
        }

        public TripDao(ConnectionSource connectionSource, DatabaseTableConfig<TripEntry> tableConfig)
                throws SQLException {
            super(connectionSource, tableConfig);
        }


        @Override
        public long createIfNotExists(Context context, TripEntry trip) throws SQLException {
            TripEntry entry = super.createIfNotExists(trip);

            if (entry == null) {
                throw new SQLException("Failed to insert trip - returned trip is null.");
            }
            return entry.getId();
        }

    }
}